package com.lauor.test.plugins;

import com.lauor.smpedr.plugin.Interceptor;
import com.lauor.smpedr.plugin.InterceptorScopeEnum;
import com.lauor.smpedr.plugin.Invocation;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数据持久框架edr 拦截器
 */
public class UserInterceptor implements Interceptor {

    @Override
    public boolean before(Invocation invocation) {//目标方法执行前拦截
        System.out.println( String.format("拦截器拦截方法名:%s", invocation.getMethod().getName()) );
        Object[] objects = invocation.getArgs();
        if (objects != null){
            for (Object object : objects) {
                System.out.println( String.format("拦截器拦截方法参数:%s", object) );
            }
        }
        //true:继续执行方法， false:终止执行
        return true;
    }

    @Override//目标方法执行后拦截
    public void after(Invocation invocation) {

    }

    /**
     * @description:拦截的目标方法
     * @see com.lauor.smpedr.session.SqlSession
     * @see com.lauor.smpedr.core.executor.Executor
     */
    @Override
    public Set<String> targetMethods() {
        return Stream.of("delete").collect(Collectors.toSet());
    }

    @Override
    public List<InterceptorScopeEnum> getInterceptorScopes() {
        //拦截目标
        return Arrays.asList( InterceptorScopeEnum.EXECUTOR );
    }
}
